<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\product\Cart;
use app\admin\model\product\Sku;
use app\admin\model\product\Value;
use app\common\controller\Api;
use think\Controller;
use think\Db;
use think\Request;


class Productbrowser extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
//        self::$model = new Cart();
    }

    /**
     * 商品浏览记录
     */
    public function shopBrowser()
    {
        $page = input('page');
        $start = ($page - 1) * self::$totalList;

        $uid = $this->uid;
        $browInfos = Db::table('zx_browser_product')->join('zx_shop shop', 'zx_browser_product.product_id = shop.id')
            ->field('zx_browser_product.id,zx_browser_product.product_id,shop.goods_name,shop.goods_introduct,shop.goods_ico,shop.member_price')
            ->where('zx_browser_product.uid', $uid)
            ->where('shop.status', 1)
            ->order('zx_browser_product.id desc')
            ->limit($start, self::$totalList)
            ->select();

        $totalCount = Db::table('zx_browser_product')->join('zx_shop shop', 'zx_browser_product.product_id = shop.id')
            ->where('zx_browser_product.uid', $uid)
            ->count();
        if ($browInfos) {
            foreach ($browInfos as &$v) {
                $v['goods_ico'] = get_cover($v['goods_ico'], 'path');
//
//                $sku = ProductSku::where('product_id', $v->id)->orderBy('member_price', 'asc')->first();
//                if ($sku) {
//                    $member_price = $sku->member_price;
//                    $parameter = $sku->value;
//                    $parameterId = $sku->id;
//                } else {
//                    $member_price = 0.00;
//                    $parameter = '无';
//                    $parameterId = 0;
//                }
//
//                $v->price = $member_price;
//                $v->parameter = $parameter;
//                $v->parameter_id = $parameterId;
            }
        }

        $this->apiSuccess('商品浏览记录', ['data' => $browInfos, 'totalCount' => $totalCount]);
    }

    /**
     * 添加商品浏览记录
     */
    public function addShopBrowser()
    {
        $id = input('id');
        $info = Db('shop')->where('status', 1)->where('id', $id)->find();
        if (!$info) {
            $this->apiError('该商品不存在');
        }

        $uid = $this->userInfo['uid'];
        $collInfo = Db('browser_product')->where('uid', $uid)->where('product_id', $id)->find();
        if ($collInfo) {
            $this->apiError('已加入浏览记录！');
        }

        $data = [
            'uid' => $uid,
            'product_id' => $id,
            'create_time' => time(),
            'update_time' => time()
        ];
        Db('browser_product')->insert($data);
        $this->apiSuccess('成功加入浏览记录！');
    }

    /**
     * 删除商品浏览记录
     */
    public function delBrowser()
    {
        $uid = $this->userinfo['uid'];
        $ids = input('ids');

        $idsArr = array_unique(explode(',', $ids));

        $res = Db('browser_product')->where('uid', $uid)->where('id', 'in', $idsArr)->delete();
        if ($res) {
            $this->apiSuccess('删除成功');
        } else {
            $this->apiError('删除失败');
        }

    }

}
